//index.js
//获取应用实例
var app = getApp();
Page({
  data: {
    lat: '',//纬度
    lng: '',//经度
    pageNum: -1,
    shopList: [],
    topShopList: [],
    radius: 10000,
    cancelTime: null,
    text: "",
  },
  input: function (e) {
    console.log(e.detail.value)
    // var cancelTime = this.data.cancelTime
    // if (cancelTime != null)
    //   clearTimeout(cancelTime)
    var t = e.detail.value
    t = t.length < 1 ? null : t;
    // var that = this;
    // var tt = setTimeout(function () {
      // that.setData({ pageNum: -1, shopList: [], topShopList: [] })
      // that.getNextPageInfo(false);
      // that.getNextPageInfo(true);
    // }, 2000)
    this.setData({ text: t })
  },
  //查询
  search:function(){
    this.setData({ pageNum: -1, shopList: [], topShopList: [] })
    this.getNextPageInfo(false);
    this.getNextPageInfo(true);
  },
  onLoad: function () {
    var that = this;
    that.getNextPageInfo(false);
    that.getNextPageInfo(true);
    // wx.getLocation({
    //   type: "gcj02",
    //   success: function (res) {
    //     var latitude = res.latitude
    //     var longitude = res.longitude
    //     var speed = res.speed
    //     var accuracy = res.accuracy
    //     that.setData({ lat: latitude, lng: longitude})
    //     that.getNextPageInfo(false);
    //     that.getNextPageInfo(true);
    //   },
    //   fail: function (rse) {
    //     wx.showToast({
    //       title: '您未授权，查询信息将不准确',
    //       showCancel: false,
    //     })
    //     that.getNextPageInfo(false);
    //     that.getNextPageInfo(true);
    //   }
    // });
  },
  //上拉加载
  onReachBottom: function () {
    this.getNextPageInfo(true);
  },
  //下一页
  getNextPageInfo: function (isTop) {
    var that = this;
    var url = app.url.baseUrl + "/shop/near";
    var shopList = that.data.shopList;
    var obj = { lat: this.data.lat, lng: this.data.lng, radius: that.data.radius, name: this.data.text }
    if (!isTop) {
      url = app.url.baseUrl + "/shop/recommend";
      shopList = that.data.topShopList;
    } else
      obj.pageNumber = that.data.pageNum + 1;

    app.ajax(url, obj, function (res) {
      var list = res.data.data;
      list = app.notNull(list,[]);
      if (list.length == 0) {
        // wx.showToast({
        //   title: app.tips.noPageData,
        // })
        return;
      }
      that.data.pageNum += 1;
      for (var i = 0; i < list.length; i++) {
        var tempObj = list[i];
        tempObj.h=true;
        if (tempObj.distance == -1)
          tempObj.distance = "未知";
        shopList.push(tempObj);
      }

      if (!isTop)
        that.setData({ topShopList: shopList });
      else
        that.setData({ shopList: shopList });
    })
  },
  search_bg_tap: function (e) {
    console.log(e);
    wx.navigateTo({
      url: '../search/search'
    })
  }, shopInfo: function (e) {
    wx.navigateTo({
      url: '../info/info?id=' + e.currentTarget.dataset.id
    })
  },
  onShareAppMessage: function () {
    console.log("转发回调");
  },
  hs:function(e){
    var index = e.currentTarget.dataset.index;
    var s = e.currentTarget.dataset.s;
    var obj = null;
    if(s==1){
      obj = this.data.shopList[index];
      obj.h = obj.h?false:true;
      this.setData({ shopList: this.data.shopList})
    }
    else if(s==0){
      obj = this.data.topShopList[index];
      obj.h = obj.h ? false : true;
      this.setData({ topShopList: this.data.topShopList })
    }
  }
})

